package com.winit.openapi.wh.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.winit.common.query.Page;
import com.winit.common.spi.context.CommandContext;
import com.winit.oms.spi.inbound.InboundMerchandiseService;
import com.winit.oms.spi.inbound.command.QueryInboundMerchandiseCommand;
import com.winit.oms.spi.inbound.vo.InboundMerchandiseDetailsVo;
import com.winit.openapi.constants.ErrorCode;
import com.winit.openapi.controller.BaseController;
import com.winit.openapi.exception.ApiException;
import com.winit.openapi.model.RequestMsg;
import com.winit.openapi.model.ResponseMsg;
import com.winit.openapi.util.ValidateUtil;
import com.winit.openapi.wh.builder.InboundMerchandiseBuilder;

/**
 * 订单商品信息
 * 
 * @version <pre>
 * Author	Version		Date		Changes
 * ming.chen 	1.0  		2017-3-21 	Created
 * 
 * </pre>
 * @since 1.
 */
@Controller
@RequestMapping(value = "/wh/inboundMerchandise")
public class InboundMerchandiseController extends BaseController {

    @Resource
    private InboundMerchandiseService inboundMerchandiseService;

    /**
     * 查询商品信息列表
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/queryMerchandisePage")
    public String queryMerchandisePage(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        Object obj = requestMsg.getData();
        QueryInboundMerchandiseCommand command = null;
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            ValidateUtil.validNotNull(json, "orderNo");
            command = new QueryInboundMerchandiseCommand();
            command.setOrderNo(json.getString("orderNo"));
            command.setSearchable(buildSearchablePageParams(json));
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }

        command.setCtx(CommandContext.getContext());
        Page<InboundMerchandiseDetailsVo> page = inboundMerchandiseService.queryMerchandisePage(command);

        if (page != null) {
            List<Map<String, Object>> datalist = InboundMerchandiseBuilder.buildReturnMerchandiseList(page.getContent());

            Map<String, Object> pageMap = new HashMap<String, Object>();
            pageMap.put("pageNo", page.getPageable().getPageNumber());
            pageMap.put("pageSize", page.getPageable().getPageSize());
            pageMap.put("totalCount", page.getTotalElements());

            Map<String, Object> data = new HashMap<String, Object>();
            data.put("packageList", datalist);
            data.put("pageParams", pageMap);
            responseMsg.setData(data);
        }
        return SUCCESS;
    }

    /**
     * 导出订单商品信息
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/exportInboundMerchandise")
    public String exportInboundOrder(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        Object obj = requestMsg.getData();
        QueryInboundMerchandiseCommand command = null;
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            ValidateUtil.validNotNull(json, "orderNo");
            command = new QueryInboundMerchandiseCommand();
            command.setOrderNo(json.getString("orderNo"));
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        command.setCtx(CommandContext.getContext());
        List<InboundMerchandiseDetailsVo> list = inboundMerchandiseService.queryByOrder(command);

        if (list != null) {
            List<Map<String, Object>> datalist = InboundMerchandiseBuilder.buildReturnMerchandiseList(list);

            Map<String, Object> data = new HashMap<String, Object>();
            data.put("packageList", datalist);
            responseMsg.setData(data);
        }

        return SUCCESS;
    }

}
